/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * 
 * Copyright 2010 yura, Version 2.0 (the "License");
 *
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 
 * Unless required by applicable law or agreed to in writing, software
 *
 * distributed under the License is distributed on an "AS IS" BASIS,
 *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

package org.opu.doc2db.document.datadocument;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.simpleframework.xml.Root;

/**
 *  table in document, contented of rows
 * 
 * @author yura
 */
@Root(name="table")
public class Table extends ParagraphList<TableRow> {

    /**
     *  Create full table with data
     *
     * @param id id of paragraph
     * @param rows rows of table (table data)
     */
    public Table(String id,
            Collection<TableRow> rows) {
        super(id, rows);
        super.dataSeparator = "\n";
    }

    /**
     *  Create table withoute data, but with paragraph identificator
     *
     * @param id id of paragraph
     */
    public Table(String id) {
        super(id);
        super.dataSeparator = "\n";
    }

    /**
     *  Create table withoute paragraph identificator,
     * but with data (rows of table)
     *
     * @param rows table rows
     */
    public Table(Collection<TableRow> rows) {
        super(rows);
        super.dataSeparator = "\n";
    }

    /**
     *  Create table withoute paragraph identificator,
     * but with data (rows of table) present in double array
     * of strings
     *
     * @param data arrays of strings, with data of each cell in table
     */
    public Table(String [][] data){
        super(arrsToLsts(data));
        super.dataSeparator = "\n";
    }

    /**
     *  Create empty table
     */
    public Table() {
        super.dataSeparator = "\n";
    }

    /**
     *  Returns an array containing all of the elements
     * if rows list
     *
     * @return array containing all of the rows
     */
    public TableRow[] rowsToArray() {
        Object[] obj = elements.toArray();
        TableRow[] ret = new TableRow[obj.length];

        System.arraycopy(obj, 0, ret, 0, obj.length);

        return ret;
    }

    /**
     *  Add empty row to the end of rows list
     *
     * @return true if rows list changed as a result of the call
     */
    public boolean rowsAddEmpty(){
        return elementsAdd(new TableRow());
    }

    /**
     *  Add cell into end of last row
     *
     * @param cell cell the will be add
     * @return true if rows list changed as a result of the call
     */
    public boolean addInEnd(TableCell cell){
        int size = elements.size();
        TableRow r = elements.get(size);

        return r.elementsAdd(cell);
    }

    /**
     * Replicate double array of strings into list of rows of table
     * <br>
     * <pre>
     * Fore example.
     * Double array:
     * {{"1", "ivan", "ivanovich"},
     *  {"2", "fedor", "fedorovich"},
     *  {"3", "petr", "petrovich"}}
     * Transform in to
     * List&lt;TableRow&gt;:
     *  1) List&lt;TableCell&gt;:
     *      1)TextParagraph("1")
     *      2)TextParagraph("ivan")
     *      3)TextParagraph("ivanovich")
     *  2) List&lt;TableCell&gt;:
     *      1)TextParagraph("2")
     *      2)TextParagraph("fedor")
     *      3)TextParagraph("fedorovich")
     *  3) List&lt;TableCell&gt;:
     *      1)TextParagraph("3")
     *      2)TextParagraph("petr")
     *      3)TextParagraph("petrovich")
     * <pre>
     *
     * @see Table
     * @see TableRow
     * @param data arrays of strings
     * @return list of rows
     */
    public static Collection<TableRow> arrsToLsts(String[][] data) {
        ArrayList<TableRow> trows = new ArrayList<TableRow>();

        for (String[] r : data) {
            ArrayList<TableCell> cells = new ArrayList<TableCell>();

            for (String c : r) {
                TextParagraph textParagraph = new TextParagraph(c);
                List<TextParagraph> asList = Arrays.asList(textParagraph);
                TableCell tableCell = new TableCell(asList);

                cells.add(tableCell);
            }

            TableRow tableRow = new TableRow(cells);

            trows.add(tableRow);
        }

        return trows;
    }

}
